package com.scy.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.scy.common.Result;
import com.scy.entity.User;
import com.scy.mapper.UserMapper;
import com.scy.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.List;

/**
 * 本类用来演示
 *
 * @author wwh
 * @date 2024/6/25 15:21 $
 */
@Controller
@RequestMapping("myuser")
public class UserController {

    @Resource
    UserService userService;

//    http://127.0.0.1:8088/myuser/find/1
    @RequestMapping("/find/{id}")
    @ResponseBody
    public Result findUser(@PathVariable Integer id){

        return Result.ok("ok",userService.findUserById(id));

    }

    @GetMapping("/init")
    public ModelAndView findAll(){

        ModelAndView modelAndView = new ModelAndView();

        modelAndView.addObject("users",userService.findAll());

        modelAndView.setViewName("/user/user-list");

        return modelAndView;
    }

    @PostMapping ("/add")
    @ResponseBody
    public Result addUser(@RequestBody User user){

        userService.addUser(user);

        return Result.ok("ok",user);
    }

    @PutMapping("/update/{id}")
    @ResponseBody
    public Result addUser(@PathVariable Long id,@RequestBody User user){

        user.setId(id);

        userService.updateUser(user);

        return Result.ok("ok",user);
    }

    @DeleteMapping("/delete/{id}")
    @ResponseBody
    public Result addUser(@PathVariable Long id){

        userService.delUser(id);

        return Result.ok("ok",null);
    }
}
